﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Domain.Models;
using System.Data;
using Utilities;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

namespace Domain.DAL
{
    public class UserDAO
    {

        #region Private Fields

        private Database db;

        #endregion

        #region Constructors

        public UserDAO(Database theDatabase)
        {
            this.db = theDatabase;
        }

        #endregion

        #region Public Methods

        public User Get(int id)
        {
            User theUser = new User();
            DataSet ds = new DataSet();

            try
            {
                ds = this.db.ExecuteDataSet("sp_Users_Get", id);
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"] != DBNull.Value)
                    {
                        theUser.ID = Convert.ToInt32(ds.Tables[0].Rows[0]["ID"]);
                    }
                    if (ds.Tables[0].Rows[0]["Username"] != null && ds.Tables[0].Rows[0]["Username"] != DBNull.Value)
                    {
                        theUser.UserName = ds.Tables[0].Rows[0]["UserName"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["Password"] != null && ds.Tables[0].Rows[0]["Password"] != DBNull.Value)
                    {
                        theUser.Password = ds.Tables[0].Rows[0]["Password"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["EmployeeID"] != null && ds.Tables[0].Rows[0]["EmployeeID"] != DBNull.Value)
                    {
                        theUser.EmployeeID = Convert.ToInt32(ds.Tables[0].Rows[0]["EmployeeID"]);
                    }
                    if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"] != DBNull.Value)
                    {
                        theUser.IsDeleted = ds.Tables[0].Rows[0]["IsDeleted"].ToString();
                    }
                }
            }
            catch (Exception)
            {
                ds.Dispose();
                throw;
            }

            return theUser;
        }

        public int Insert(User theUser)
        {
            int result = MyConfig.NullNumber;

            try
            {
                result = Convert.ToInt32(this.db.ExecuteScalar("sp_Users_Insert", theUser.UserName, theUser.Password, theUser.EmployeeID));
            }
            catch (Exception)
            {
                throw;
            }
            return result;
        }

        public int Update(User theUser)
        {
            int result = MyConfig.NullNumber;

            try
            {
                result = Convert.ToInt32(this.db.ExecuteScalar("sp_Users_Update", theUser.ID, theUser.UserName, theUser.Password, theUser.EmployeeID));
            }
            catch (Exception)
            {
                throw;
            }
            return result;
        }

        public bool Delete(string id)
        {
            bool result = false;

            try
            {
                result = this.db.ExecuteScalar("sp_Users_Delete", id.ToString()).ToString() == id;
            }
            catch (Exception)
            {
                throw;
            }
            return result;
        }

        public DataSet Search(User theUser)
        {
            DataSet ds = new DataSet();

            try
            {
                ds = this.db.ExecuteDataSet("sp_Users_List", theUser.ID, theUser.UserName, theUser.Password, theUser.EmployeeID);
            }
            catch (Exception)
            {
                ds.Dispose();
                throw;
            }

            return ds;
        }

        public User ValidateUser(string username, string password)
        {
            User theUser = new User();
            DataSet ds = new DataSet();

            try
            {
                ds = this.db.ExecuteDataSet("sp_Users_ValidateUser", username, password);
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"] != DBNull.Value)
                    {
                        theUser.ID = Convert.ToInt32(ds.Tables[0].Rows[0]["ID"]);
                    }
                    if (ds.Tables[0].Rows[0]["UserName"] != null && ds.Tables[0].Rows[0]["UserName"] != DBNull.Value)
                    {
                        theUser.UserName = ds.Tables[0].Rows[0]["UserName"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["Password"] != null && ds.Tables[0].Rows[0]["Password"] != DBNull.Value)
                    {
                        theUser.Password = ds.Tables[0].Rows[0]["Password"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["EmployeeID"] != null && ds.Tables[0].Rows[0]["EmployeeID"] != DBNull.Value)
                    {
                        theUser.EmployeeID = Convert.ToInt32(ds.Tables[0].Rows[0]["EmployeeID"]);
                    }
                    if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"] != DBNull.Value)
                    {
                        theUser.IsDeleted = ds.Tables[0].Rows[0]["IsDeleted"].ToString();
                    }
                }
            }
            catch (Exception)
            {
                ds.Dispose();
                throw;
            }

            return theUser;
        }

        public bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            bool result = false;

            try
            {
                if (!this.db.ExecuteScalar("sp_Users_ChangePassword", username, oldPassword, newPassword).ToString().Equals(MyConfig.NullNumber.ToString()))
                {
                    result = true;
                }
                    
            }
            catch (Exception)
            {
                throw;
            }

            return result;
        }

        public string GetRoles(int userID)
        {
            string result = string.Empty;

            try
            {
                result = this.db.ExecuteScalar("sp_Users_GetRoles", userID).ToString();
            }
            catch (Exception)
            {
                throw;
            }
            return result;
        }

        public User GetByUsername(string username)
        {
            User theUser = new User();
            DataSet ds = new DataSet();

            try
            {
                ds = this.db.ExecuteDataSet("sp_Users_GetByUsername", username);
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"] != DBNull.Value)
                    {
                        theUser.ID = Convert.ToInt32(ds.Tables[0].Rows[0]["ID"]);
                    }
                    if (ds.Tables[0].Rows[0]["Username"] != null && ds.Tables[0].Rows[0]["Username"] != DBNull.Value)
                    {
                        theUser.UserName = ds.Tables[0].Rows[0]["Username"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["Password"] != null && ds.Tables[0].Rows[0]["Password"] != DBNull.Value)
                    {
                        theUser.Password = ds.Tables[0].Rows[0]["Password"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["EmployeeID"] != null && ds.Tables[0].Rows[0]["EmployeeID"] != DBNull.Value)
                    {
                        theUser.EmployeeID = Convert.ToInt32(ds.Tables[0].Rows[0]["EmployeeID"]);
                    }
                    if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"] != DBNull.Value)
                    {
                        theUser.IsDeleted = ds.Tables[0].Rows[0]["IsDeleted"].ToString();
                    }
                }
            }
            catch (Exception)
            {
                ds.Dispose();
                throw;
            }

            return theUser;
        }

        #endregion

        #region Public Static Methods

        public static UserDAO GetInstance()
        {
            return EnterpriseLibraryContainer.Current.GetInstance<UserDAO>();
        }

        #endregion
    }
}
